<template>
  <van-nav-bar
    title="新增地址"
    left-text="返回"
    left-arrow
    @click-left="back"
  />
  <van-address-edit
    :area-list="areaList"
    show-postal
    show-delete
    show-set-default
    show-search-result
    :search-result="searchResult"
    :area-columns-placeholder="['请选择', '请选择', '请选择']"
    @save="onSave"
    @delete="onDelete"
    @change-detail="onChangeDetail"
  />
</template>

<script setup>
import { Toast } from "vant";
import { postToken } from "../../../utils/request";
import { ref, reactive } from "vue";
import { areaList } from "@vant/area-data";
import { useRouter } from "vue-router";

const router = useRouter();
// 定义请求数据格式
let params = reactive({
  name: "",
  phone: "",
  password: "123456",
  area: "",
  area_name: "",
  desc: "",
  post_code: "",
});
// 新增地址请求后台
const onSave = async (data) => {
  console.log(data);
  params.name = data.name;
  params.phone = data.tel;
  params.area = data.areaCode;
  params.area_name = data.city + data.county;
  params.desc = data.addressDetail;
  params.post_code = data.postalCode;
  console.log(params);
  let res;
  try {
    res = await postToken("/address", params);
    console.log(123);
    Toast("地址新增成功");
    router.push("/address");
  } catch (error) {
    Toast("地址新增失败");
  }
};
// 返回按钮
const back = () => history.back();
</script>

<style lang="scss" scoped></style>
